home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 1 / Gekikoh Dennoh Club Vol. 1 (Japan).7z / Gekikoh Dennoh Club Vol. 1 (Japan) (Track 1).bin / kowin / archive / net / koprosrc.lzh / receive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-15  |  4.1 KB  |  220 lines

  1. /*    Copyright 1992 H.Ogasawara(COR.)    */
  2.  
  3. #include    "proto.h"
  4. #include    "run.h"
  5.  
  6. /*
  7. 3 modes only!
  8.  
  9.     X-MODEM SUM
  10.     X/Y-MODEM(CRC)
  11.     Y-MODEM-G
  12. */
  13. static void    ReceiveRun3(),
  14.         ReceiveRun2(),
  15.         ReceiveRun1();
  16. void        ReceiveStart();
  17.  
  18. enum    { retSUM, retCRC, retG };
  19.  
  20. #define    GWAIT    (16*100)
  21.  
  22. unsigned short    rettype= retCRC;
  23. unsigned int    firstcode,    /* size */
  24.         firstsize;
  25.  
  26. static char    xmodemfile[80];    /* 1992 9/15 */
  27.  
  28. Write232C( code )
  29. unsigned char    code;
  30. {
  31.     unsigned int    time= ONTIME()+ 6000;
  32.     while( !OSNS232C() ){
  33.         if( (B_KEYSNS()&0xff) == 0x03 ){
  34.             if( (B_KEYINP()&0xff) == 0x03 ){
  35.                 allbreak();
  36.                 return    -1;
  37.             }
  38.         }else if( ONTIME() > time ){
  39.             allbreak();
  40.             return    -1;
  41.         }
  42.     }
  43.     OUT232C( code );
  44.     return    0;
  45. }
  46.  
  47. ReceiveEnd()
  48. {
  49.     if( batch )
  50.         ReceiveStart();
  51.     else
  52.         EndProgram();
  53. }
  54.  
  55. /* Get Packet DATA & check Y */
  56. static void
  57. ReceiveRun4( ptr )
  58. unsigned char    *ptr;
  59. {
  60.     if( DataUnpack( firstsize, ptr, rettype == retSUM ) < 0 ){
  61.     /* Error */
  62.         if( rettype != retG ){
  63.             if( Write232C( NAK ) < 0 )
  64.                 return;
  65.         }else{
  66.             Write232C( CAN );
  67.             allbreak();
  68.             Write232C( CAN );
  69.             return;
  70.         }
  71.         run= ReceiveRun3;
  72.         timeout= GWAIT+ONTIME();
  73.         runsize= 1;
  74.     }else{
  75.     /* OK */
  76.         if( rettype != retG )
  77.             if( Write232C( ACK ) < 0 )
  78.                 return;
  79.         if( *ptr == 0 && firstsize == 128 ){    /* Y-MODEM */
  80.             if( batch ){
  81.                 unsigned short    i;
  82.                 for( i= 0 ; i< 128 ; i++ ){
  83.                     if( (ptr+SKIP)[i] )
  84.                         goto    notend;
  85.                 }
  86.                 batch= 0;
  87.                 timeout= runsize= 0;
  88.                 ReceiveEnd();
  89.                 return;
  90.             }
  91.         notend:
  92.             filetime= GetFilePacket( ptr+SKIP, filename, &filesize );
  93.             last= (filesize>>7)+1;
  94.             if( !OpenFileW( filename ) ){
  95.                 Write232C( CAN );
  96.                 Write232C( CAN );
  97.                 return;
  98.             }
  99.             Write232C( firstcode );
  100.             run= ReceiveRun1;
  101.         }else{
  102. /*            strcpy( filename, DEFAULTNAME ); 1992 9/15 */
  103.             strcpy( filename, *xmodemfile ? xmodemfile : DEFAULTNAME );
  104.             if( !OpenFileW( filename ) ){
  105.                 Write232C( CAN );
  106.                 Write232C( CAN );
  107.                 return;
  108.             }
  109.             filetime= filesize= 0;
  110.             if( WriteFile( ptr+SKIP, firstsize ) < 0 )
  111.                 return;
  112.             block+= firstsize>>7;
  113.             size+= firstsize;
  114.             run= ReceiveRun3;
  115.         }
  116.         DispFilename();
  117.         DispCountup();
  118.         timeout= GWAIT+ONTIME();
  119.         runsize= 1;
  120.     }
  121. }
  122.  
  123. /* Get DATA HEAD */
  124. static void
  125. ReceiveRun3( ptr )
  126. unsigned char    *ptr;
  127. {
  128.     if( !ptr ){
  129.         Write232C( CAN );
  130.         allbreak();
  131.         Write232C( CAN );
  132.     }else{
  133.         if( *ptr == SOH || *ptr == STX ){
  134.             firstsize= *ptr == SOH ? 128 : 1024;
  135.             run= ReceiveRun2;
  136.             runsize= firstsize+SKIP+(rettype == retSUM ? 1 : 2);
  137.             timeout= 0;
  138.         }else if( *ptr == EOT ){
  139.             Write232C( ACK );
  140.             CloseFile();
  141.             timeout= runsize= 0;
  142.             ReceiveEnd();
  143.         }else{
  144.             if( *ptr != CAN )
  145.                 Write232C( CAN );
  146.             allbreak();
  147.         }
  148.     }
  149. }
  150.  
  151. /* Get Packet DATA */
  152. static void
  153. ReceiveRun2( ptr )
  154. unsigned char    *ptr;
  155. {
  156.     if( DataUnpack( firstsize, ptr, rettype == retSUM ) < 0 ){
  157.         if( rettype != retG ){
  158.             if( Write232C( NAK ) < 0 )
  159.                 return;
  160.         }else{
  161.             Write232C( CAN );
  162.             allbreak();
  163.             Write232C( CAN );
  164.             return;
  165.         }
  166.         run= ReceiveRun3;
  167.         timeout= GWAIT+ONTIME();
  168.         runsize= 1;
  169.     }else{
  170.         if( rettype != retG )
  171.             if( Write232C( ACK ) < 0 )
  172.                 return;
  173.         if( WriteFile( ptr+SKIP, firstsize ) < 0 )
  174.             return;
  175.         run= ReceiveRun3;
  176.         timeout= GWAIT+ONTIME();
  177.         runsize= 1;
  178.         block+= firstsize>>7;
  179.         size+= firstsize;
  180.         DispCountup();
  181.     }
  182. }
  183.  
  184. /* Check TOP Packet */
  185. static void
  186. ReceiveRun1( ptr )
  187. unsigned char    *ptr;
  188. {
  189.     if( !ptr ){
  190.         Write232C( firstcode );
  191.     }else{
  192.         if( *ptr == SOH || *ptr == STX ){
  193.             firstsize= *ptr == SOH ? 128 : 1024;
  194.             run= *filename ? ReceiveRun2 : ReceiveRun4;
  195.             runsize= firstsize+SKIP+(rettype == retSUM ? 1 : 2);
  196.             timeout= 0;
  197.         }else{
  198.             if( *ptr != CAN && *ptr != EOT )
  199.                 Write232C( CAN );
  200.             allbreak();
  201.         }
  202.     }
  203. }
  204.  
  205. void
  206. ReceiveStart()
  207. {
  208.     static unsigned char    first[3]= { NAK, __C, __G };
  209.     firstcode= first[ rettype ];
  210.     if( Write232C( firstcode ) < 0 )
  211.         return;
  212.     run= ReceiveRun1;
  213.     timeout= 930+ONTIME();
  214.     strcpy( xmodemfile, filename );
  215.     *filename= '\0';
  216.     block= size= last= 0;
  217.     runsize= 1;
  218. }
  219.  
  220.